package com.kenliang.algorithm.sort;

import org.junit.Assert;
import org.junit.Test;

import java.util.Arrays;

/**
 * @author KenLiang
 * @description:
 * @date Created in 3:04 PM 2021/1/13.
 */
public class SortChecker {

    @Test
    public void bubblingSortTest(){
        this.check(new BubblingSort());
    }

    @Test
    public void selectSortTest(){
        this.check(new SelectSort());
    }

    @Test
    public void insertSortTest(){
        this.check(new InsertSort());
    }

    @Test
    public void quickSortTest(){
//        this.checkOnce(new QuickSort());
        this.check(new QuickSort());
    }

    @Test
    public void heapSortTest(){
//        this.checkOnce(new HeapSort());
        this.check(new HeapSort());
    }

    @Test
    public void mergeSortTest(){
        this.checkOnce(new MergeSort());
        //容易栈溢出
//        this.check(new MergeSort());
    }


    private void checkOnce(ISort sort){
        int[] out = sort.sort(new int[]{31,10,8,85,2,41,16,-45,-12,10,27,-6});
        System.out.println("---------------------");
        sort.soutArray(out);
    }
    private void check(ISort sort){
//        int testTime = 500000;
        int testTime = 500000;
        int maxSize = 100;
        int maxValue = 100;
        boolean succeed = true;
        System.out.println("test begin");
        for (int i = 0; i < testTime; i++) {
            int[] arr1 = generateRandomArray(maxSize, maxValue);
            sort.soutArray(arr1);
            int[] arr2 = copyArray(arr1);
            sort.sort(arr1);
            Arrays.sort(arr2);
            if (!isEqual(arr1, arr2)) {
                succeed = false;
                System.out.println("arr1==================");
                sort.soutArray(arr1);
                System.out.println("arr2==================");
                sort.soutArray(arr2);
                break;
            }
        }
        System.out.println("test end");
        System.out.println("测试" + testTime + "组是否全部通过：" + (succeed ? "是" : "否"));
        Assert.assertTrue(succeed);
    }
    public static boolean isEqual(int[] arr1, int[] arr2) {
        if ((arr1 == null && arr2 != null) || (arr1 != null && arr2 == null)) {
            return false;
        }
        if (arr1 == null && arr2 == null) {
            return true;
        }
        if (arr1.length != arr2.length) {
            return false;
        }
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i] != arr2[i]) {
                return false;
            }
        }
        return true;
    }

    // 生成随机数组（用于测试）
    public static int[] generateRandomArray(int maxSize, int maxValue) {
        int[] arr = new int[(int) ((maxSize + 1) * Math.random())];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random());
        }
        return arr;
    }

    // 拷贝数组（用于测试）
    public static int[] copyArray(int[] arr) {
        if (arr == null) {
            return null;
        }
        int[] res = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            res[i] = arr[i];
        }
        return res;
    }
}
